﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.IO;

namespace PDChart
{

    public partial class PDChart : UserControl
    {
        public PDChart()
        {
 

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            
            this.BackColor = Color.Gray;

            TextFont = this.Font;
            LabelFont = this.Font;

            Amps = new double[] { 1, 2, 5, 10 };
            InitializeComponent();
        }


        public virtual bool LoadFile(string filename)
        {
            return true;
        }


        public Font TextFont;
        public Font LabelFont;


        public XYZ MaxValue = new XYZ(0, 0, 0);

        public double[,] OrigData = new double[0, 0];

        protected double[,] Data = new double[0, 0];

        protected void DrawText(Graphics g)
        {
            using (SolidBrush brush = new SolidBrush(Color.White))
            {
                SizeF size = g.MeasureString(this.Text, this.TextFont);
                g.DrawString(this.Text, this.TextFont, brush, new PointF(this.Width / 2 - size.Width / 2, size.Height / 2));
                
            }
        }

        protected void DrawZoomButtonBorder()
        {
            using (Graphics g = CreateGraphics())
            {
                DrawZoomButtonBorder(g);
            } 
        }

        protected void DrawZoomButtonBorder(Graphics g)
        {
            Border3DStyle style = MouseInZoomButton ? Border3DStyle.Raised : Border3DStyle.Sunken;

            
            ControlPaint.DrawBorder3D(g, ZoomButtonRectangle, style, Border3DSide.Left | Border3DSide.Right | Border3DSide.Bottom | Border3DSide.Top);
            
        }


        protected void DrawZoomButtonText()
        {
            using (Graphics g = CreateGraphics())
            {
                DrawZoomButtonText(g);
            }
        }

        protected void DrawZoomButtonText(Graphics g)
        {
            using (SolidBrush bgbrush = new SolidBrush(this.BackColor))
            {
                g.FillRectangle(bgbrush, Rectangle.Inflate(ZoomButtonRectangle, -2, -2));
            }

            string text = "X" + Amps[AmpIndex];

            SizeF sz = g.MeasureString(text, this.TextFont);

            PointF loc = ZoomButtonRectangle.Location;
 
            loc.X = loc.X + ZoomButtonRectangle.Width / 2 - sz.Width / 2;
            
            loc.Y = loc.Y + ZoomButtonRectangle.Height / 2 - sz.Height / 2;

            using (SolidBrush brush = new SolidBrush(Color.White))
            {
                g.DrawString(text, this.TextFont, brush, loc);
            }

        }

        public double GetCurrentAmp()
        {
            return Amps[AmpIndex];
        }

        protected Rectangle ZoomButtonRectangle = new Rectangle(100, 10, 50, 20);

        protected double[] Amps;
        protected int AmpIndex = 0;
        protected bool MouseInZoomButton = false;

        protected override void OnMouseMove(MouseEventArgs e)
        {
            MouseInZoomButton = ZoomButtonRectangle.Contains(e.X, e.Y);

            DrawZoomButtonBorder();

            base.OnMouseMove(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (ZoomButtonRectangle.Contains(e.X, e.Y))
            {
                AmpIndex = (AmpIndex == Amps.Length - 1) ? 0 : AmpIndex + 1;
                this.Invalidate();
            }

            base.OnMouseClick(e);
        }

        public void Set(double[,] data)
        {
            Data = data;   
            this.Invalidate();
        }


        protected virtual void OnDraw(Graphics g)
        {

        }

        protected override void OnPaint(PaintEventArgs e)
        {

            Graphics g = e.Graphics;
           
            OnDraw(g);

            DrawZoomButtonBorder(g);
            DrawZoomButtonText(g);

            base.OnPaint(e);

        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            this.Invalidate();
            base.OnEnabledChanged(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.Height > 100)
            {
                this.TextFont = new Font(this.Font.FontFamily, this.Height / 20);
                this.LabelFont = new Font(this.Font.FontFamily, this.Height / 30);
                this.ZoomButtonRectangle = new Rectangle(this.Width - 50, this.TextFont.Height / 2, 36, this.TextFont.Height);
                DrawZoomButtonBorder();
                DrawZoomButtonText();
            }

            this.Invalidate();

            base.OnSizeChanged(e);
        }
    }
}
